package org.webcat.electronicsubmissionplugin.wizard;

import java.awt.Component;
import java.awt.Dialog;
import java.text.MessageFormat;
import javax.swing.JComponent;
import org.openide.DialogDisplayer;
import org.openide.WizardDescriptor;
import org.openide.util.HelpCtx;
import org.openide.util.actions.CallableSystemAction;
import org.webcat.electronicsubmissionplugin.model.SubmitterModel;
import org.webcat.electronicsubmissionplugin.utils.BrowserLauncher;
import org.webcat.electronicsubmissionplugin.utils.Utils;

/**
 * Class used by netbeans to instantiate the wizard. Can be called by other buttons
 * to run the wizard.
 * @author rob44, rstodd13, samccarn
 */
public final class SubmitWizardAction extends CallableSystemAction {

    @SuppressWarnings("rawtypes")
    private WizardDescriptor.Panel[] panels;

    @Override
    public void performAction() {
        @SuppressWarnings("unchecked")
        WizardDescriptor wizardDescriptor = new WizardDescriptor(getPanels());
        // {0} will be replaced by WizardDesriptor.Panel.getComponent().getName()
        wizardDescriptor.setTitleFormat(new MessageFormat("{0}"));
        wizardDescriptor.setTitle("Electronic Submission"); // title of wizard is set here
        Dialog dialog = DialogDisplayer.getDefault().createDialog(wizardDescriptor);
        dialog.setVisible(true);
        dialog.toFront();
        boolean cancelled = wizardDescriptor.getValue() != WizardDescriptor.FINISH_OPTION;
        if (cancelled) {
            SubmitterModel.getInstance().killLongRunningTasks(); // kill off any tasks running on exit
        }
    }

    /**
     * Initialize panels representing individual wizard's steps and sets
     * various properties for them influencing wizard appearance.
     */
    @SuppressWarnings("rawtypes")
    private WizardDescriptor.Panel[] getPanels() {
        if (panels == null) {
            panels = new WizardDescriptor.Panel[]{
                new SubmitWizardPanel1(),
                new SubmitWizardPanel2()
            };
            String[] steps = new String[panels.length];
            for (int i = 0; i < panels.length; i++) {
                Component c = panels[i].getComponent();
                // Default step name to component name of panel. Mainly useful
                // for getting the name of the target chooser to appear in the
                // list of steps.
                steps[i] = c.getName();
                if (c instanceof JComponent) { // assume Swing components
                    JComponent jc = (JComponent) c;
                    // Sets step number of a component
                    // if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_*:
                    jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i));
                    // Sets steps names for a panel
                    jc.putClientProperty("WizardPanel_contentData", steps);
                    // Turn on subtitle creation on each step
                    jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE);
                    // Show steps on the left side with the image on the background
                    jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE);
                    // Turn on numbering of all steps
                    jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE);
                }
            }
        }
        return panels;
    }

    /**
     * Netbeans method. Sets the name of the wizard.
     * @return the name
     */
    @Override
    public String getName() {
        return "Start Sample Wizard";
    }

    /**
     * Unused.
     * @return null
     */
    @Override
    public String iconResource() {
        return null;
    }

    /**
     * Unused.
     * @return default help context
     */
    @Override
    public HelpCtx getHelpCtx() {
        return HelpCtx.DEFAULT_HELP;
    }

    /**
     * Unused.
     * @return false
     */
    @Override
    protected boolean asynchronous() {
        return false;
    }
}
